package chances.epg.data.taglib;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import chances.epg.commons.utils.DateUtils;
import chances.epg.entity.content.EpgSchedule;
import chances.epg.entity.content.ScheduleTimer;

public class PlayStatusFunction {  
	public static final SimpleDateFormat SDF = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	
	/**
	 * 获取节目单的播放状态  0 正播，-1 已播，1 未播
	 * @param schedule
	 * @return int
	 */
    public static int getPlayStatus(EpgSchedule schedule) {
        Date now = new Date();
        Date beginTime = schedule.getStartTime();
        Date endTime = schedule.getEndTime();
        if(now.getTime() >= endTime.getTime()){
        	return -1;
        }else if(now.getTime() < beginTime.getTime()){
        	return 1;
        }else{
        	return 0;
        }
    } 
    
    /**
     * 通过传入的字符串格式获取日期字符串
     * @param schedule
     * @param dateFormat
     * @return String
     */
    public static String formatDate(Date date,String dateFormat){
    	SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
    	return DateUtils.parseDateToStr(date,sdf);
    }
    
    /**
     * 当前时间往前推perHour小时，往后加nextHour小时
     * @param prevHour
     * @param nextHour
     * @return scheduleTimer
     */
    public static ScheduleTimer getScheduleTimerByHour(int prevHour,int nextHour){
    	ScheduleTimer scheduleTimer = new ScheduleTimer();
    	
    	Calendar calendar = Calendar.getInstance();
    	calendar.setTime(new Date());
    	calendar.add(Calendar.HOUR, -prevHour);
    	scheduleTimer.setStartTime(calendar.getTime());
    	
    	calendar.setTime(new Date());
    	calendar.add(Calendar.HOUR, nextHour);
    	scheduleTimer.setEndTime(calendar.getTime());
    	
    	return scheduleTimer;
    }
    
    /**
     * 指定时间setTime往前推perHour小时，往后加nextHour小时
     * @param setTime
     * @param prevHour
     * @param nextHour
     * @return
     */
    public static ScheduleTimer getScheduleTimerByHour(Date setTime,int prevHour,int nextHour){
    	ScheduleTimer scheduleTimer = new ScheduleTimer();
    	
    	Calendar calendar = Calendar.getInstance();
    	calendar.setTime(setTime);
    	calendar.add(Calendar.HOUR, -prevHour);
    	scheduleTimer.setStartTime(calendar.getTime());
    	calendar.setTime(setTime);
    	calendar.add(Calendar.HOUR, nextHour);
    	scheduleTimer.setEndTime(calendar.getTime());
    	
    	return scheduleTimer;
    }
    
    /**
     * 获取当天的开始与结束时间 例如2016-12-15 00：00：00，2016-12-15：23:59:59
     * @return scheduleTimer
     */
    public static ScheduleTimer getScheduleTimerByDay(){
    	return getScheduleTimerByDay(new Date());
    }
    
    /**
     * 获取某一天的结束 开始时间
     * @return
     */
    public static ScheduleTimer getScheduleTimerByDay(Date date){
    	ScheduleTimer scheduleTimer = new ScheduleTimer();
    	
    	Calendar calendar = Calendar.getInstance();
    	calendar.setTime(date);
    	
    	calendar.set(Calendar.HOUR, 0);
    	calendar.set(Calendar.MINUTE, 0);
    	calendar.set(Calendar.SECOND,0);
    	scheduleTimer.setStartTime(calendar.getTime());
    	
    	calendar.set(Calendar.HOUR, 23);
    	calendar.set(Calendar.MINUTE, 59);
    	calendar.set(Calendar.SECOND,59);
    	scheduleTimer.setEndTime(calendar.getTime());
    	
    	return scheduleTimer;
    }
    
    /**
     * 获取某天的结束开始时间   dayNum为-1表示昨天  1表示明天
     * @param dayNum
     * @return
     */
    public static ScheduleTimer getScheduleTimerByDay(int dayNum){
    	Calendar calendar = Calendar.getInstance();
    	calendar.setTime(new Date());
    	calendar.add(Calendar.DATE, dayNum);
    	return getScheduleTimerByDay(calendar.getTime());
    }
    
    
}